import { createApp } from 'vue';
import './style.css';
import App from './App.vue';

// 路由
import router from '@/router/index.js';
// api
import api from '@/api/index.js'
// vant
import vant from 'vant';
import 'vant/lib/index.css';
// cookies
import VueCookes from 'vue3-cookies';

import { showFailToast } from 'vant';

let app = createApp(App)
.use(VueCookes)
.use(api)
.use(vant)
.use(router)
.mount('#app');

app.$router.beforeEach(async(to, from, next) => {
    let meta = to.meta.Login? to.meta.Login:false;
    document.querySelector("body").setAttribute("style", "overflow: auto !important;")
    // 判断是否需要登入
    if (meta) {
        // 判断有没有cookies
        if (app.$cookies.get('User')) {
            let res = await app.$api.isLogin(app.$cookies.get('User'));
            if(res.code == 0){
                // 删除假的cookies信息
                app.$cookies.remove('User')
                showFailToast({ message: res.msg });
                next("/business/login");
            }
            if (res.code == -1) {
                // 负值为响应拦截器拦截
                showFailToast({ message: res.msg });
            }
            // 根据返回的数据判断
            if (res.code == 1) {
                app.$cookies.set('User', res.data);
                next();
            }
        }else{
            showFailToast({ message: "请登入" });
            next("/business/login");
        }
    }else{
        next();
    }
});


